<?php

namespace App\Services;

use OSS\Core\OssException;
use OSS\OssClient;

class AliyunUpload
{
    public static function upFile($path, $type)
    {
        $accessKeyId = env('ALIYUN_ACCESS_ID');
        $accessKeySecret = env('ALIYUN_ACCESS_KEY');
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = env('ALIYUN_ENDPOINT');
        // 存储空间名称
        $bucket = env('ALIYUN_BUCKET');
        //图片名称
        $name = time() . rand(1000, 9999) . '.' .$type;
        $options = array(
            OssClient::OSS_CHECK_MD5 => true,
            OssClient::OSS_PART_SIZE => 1,
        );
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $name, $path, $options);
            return $result['info']['url'];
        } catch (OssException $e) {
            return false;
        }
    }
}
